<?php
/**
 * 物流控制器
 * @author ezhu <ezhu@jipukeji.com>
 */

namespace Shop\Controller;

class TransportController extends CommonController{
    
    
    /**
     * 运费模板列表
     */
    public function index(){
        $lists = D('Transport')->getList($this->store_id);
        $meta_title = '运费模板';
        $this->assign('meta_title',$meta_title);
        $this->assign('lists',$lists);
        $this->display();
    }
    
    
    /**
     * 编辑、新增运费模板
     */
    public function edit(){
        $id = I('request.id');
        $map['id'] = $id;
        $map['store_id'] = $this->store_id;
        $model = M('Transport');
        $extend = M('TransportExtend');
        if(IS_POST && IS_AJAX){
            
            $rules = $this->rules();
            if (!$model->validate($rules)->create()){
                $this->error($model->getError());
            }
            
            $data['id'] = $id;
            $data['store_id'] = $this->store_id;
            $data['type'] = I('price_type');
            $data['title'] = I('title');
            $data['utime'] = NOW_TIME;
            
            // 开启事物
            $model->startTrans();
            if($id){
                $rst = $model->where(array('id'=>$id))->save($data);
                $type = '编辑';
                if($rst){
                    // 删除原有的
                    $delMap['transport_id'] = $id;
                    $rst = $extend->where($delMap)->delete();
                    if($rst){
                        $trans_list = $this->getData($id);
                        $rst = $extend->addAll($trans_list);
                        if($rst){
                            $model->commit();
                        }else{
                            $model->rollback();
                        }
                    }else{
                        $model->rollback();
                    }
                }else{
                    $model->rollback();
                }
            }else{
                $rst = $model->add($data);
                $type = '添加';
                if($rst !== false){
                    $trans_list = $this->getData($rst);
                    $rst = $extend->addAll($trans_list);
                    if($rst){
                        $model->commit();
                    }else{
                        $model->rollback();
                    }
                }else{
                    $model->rollback();
                }
            }
            
            $rst !== false ? $this->success($type.'成功！',U('index')) : $this->error($type.'失败！');
            exit;
        }
        $data = array();
        $meta_title = '添加运费模板';
        
        if($id){
            $data = $model->where($map)->find();
            $extList = $extend->where(array('transport_id'=>$id))->select();
            $data['express_start'] = $extList[0]['express_start'];
            $data['express_postage'] = $extList[0]['express_postage'];
            $data['express_plus'] = $extList[0]['express_plus'];
            $data['express_postageplus'] = $extList[0]['express_postageplus'];
            unset($extList[0]);
            $data['extend'] = $extList;
            $meta_title = '编辑运费模板';
        }
        $this->assign('data',$data);
        $this->assign('meta_title',$meta_title);
        $this->display();
    }
    
    
    
    /**
     * 删除运费模板
     */
    public function del(){
        $id = I('request.ids');
        if(empty($id)){
            $this->error('请选择要操作的对象');
        }
        $map['id'] = array('in',$id);
        $map['store_id'] = $this->store_id;
        $model = M('Transport');
        $model->startTrans();
        $rst = $model->where($map)->delete();
        if($rst !== false){
            //删除下面的分类
            $eMap['transport_id'] = array('in',$id);
            $res = M('TransportExtend')->where($eMap)->delete();
            if($res !== false){
                $model->commit();
                $this->success('成功删除');
                exit;
            }else{
                $model->rollback();
                $this->error('删除失败！');
            }
        }else{
            $model->rollback();
            $this->error('删除失败！');
        }
    }
    
    
    /**
     * 免运费额度设置
     */
    public function free_freight(){
        $map['store_id'] = $this->store_id;
        if(IS_POST && IS_AJAX){
            $data['free_price'] = I('free_freight');
            $rst = M('Store')->where($map)->save($data);
            if($rst !== false){
                $this->success('设置成功！');
            }else{
                $this->error('设置失败！');
            }
            exit;
        }
        $store = D('Store')->getInfo($map);
        $this->assign('data',$store);
        $this->assign('meta_title','免运费额度设置');
        $this->display();
    }
    
    
    /**
     * 运费模板地区选择
     */
    public function zone(){
        $aIds = I('aIds');
        $curIds = I('curIds');
        $no = I('no');
        if(I('type')){
            $myareas = M('ZoneUsed')->where('store_id ='.$this->store_id)->getField('areas');
        }else{
            $myareas = '';
        }
        $all_ids = explode(',',$aIds);
        $cur_ids = explode(',', $curIds);
        $hasIds = array_diff($all_ids,$cur_ids );
        
        $zone = D('Area')->areaZone($hasIds,$cur_ids, $myareas);
        $this->assign('curIds',$cur_ids);
        $this->assign('hasIds',$hasIds);
        $this->assign('zone',$zone);
        $this->assign('no',$no);
        $this->display();
    }
    /**
     * 可售区域设置，主要区别其他页面
     */
    public function Myzone(){
        $aIds = I('aIds');
        $curIds = I('curIds');
        $no = I('no');
        if(I('type')){
            $myareas = M('ZoneUsed')->where('store_id ='.$this->store_id)->getField('areas');
        }else{
            $myareas = '';
        }
        $all_ids = explode(',',$aIds);
        $cur_ids = explode(',', $curIds);
        $hasIds = array_diff($all_ids,$cur_ids );
        
        $zone = D('Area')->myZone($hasIds,$cur_ids, $myareas);
        $this->assign('curIds',$cur_ids);
        $this->assign('hasIds',$hasIds);
        $this->assign('zone',$zone);
        $this->assign('no',$no);
        $this->display();
    }

     /**
     * 商品编辑的地区选择ajax
     */
    public function get_zone(){
        if($_GET['item_id']){
            $set_zone = M('Item')->where('id ='.$_GET['item_id'])->getField('set_zone');

        }
        $set_zone && $curIds    = $set_zone ;
        $all_ids = explode(',',$aIds);
        $cur_ids = explode(',', $curIds);
        $hasIds  = array_diff($all_ids,$cur_ids);
        $myareas = M('ZoneUsed')->where('store_id ='.$this->store_id)->getField('areas');
        $zone = D('Area')->myZone($hasIds,$cur_ids , $myareas);
        $this->assign('curIds',$cur_ids);
        $this->assign('hasIds',$hasIds);
        $this->assign('zone' , $zone);
        $this->assign('list', $property_list);
        $rtn['html']    = $this->fetch();
        $this->ajaxReturn($rtn);
    }
    
    /**
     * 验证规则
     */
    public function rules(){
        $rules = array(
            array('title','require','请填写运费模板名称！'),
            array('price_type','require','请计算方式！'),
            array('express_start','require','请填写默认运费！'),
            array('express_postage','require','请填写默认运费！'),
            array('express_plus','require','请填写默认运费！'),
            array('express_postageplus','require','请填写默认运费！'),
        );
        return $rules;
    }
    
    
    /**
     * 组合运费模板数据
     * @param int $transport_id  运费模板id
     * @return array
     */
    public function getData($transport_id = 0){
        //保存默认运费
        $price_type = I('price_type');
        $trans_list = array();
        $trans_list[0]['city'] = '';
        $trans_list[0]['area_name'] = '全国';
        $trans_list[0]['price_type'] = $price_type;
        $trans_list[0]['express_start'] = I('express_start');
        $trans_list[0]['express_postage'] = I('express_postage');
        $trans_list[0]['express_plus'] = I('express_plus');
        $trans_list[0]['express_postageplus'] = I('express_postageplus');
        $trans_list[0]['transport_id'] = $transport_id;
        $trans_list[0]['province'] = '';
        
        // 查询省份id
        $proData = M('Area')->where(array('pid'=>0,'deep'=>1))->select();
        $proIds = array_column($proData,'id');
        
        //保存自定义地区的运费设置
        $areas = I('areas');
        $special = I('kd');
        if (is_array($areas) && is_array($special)){
            //$key需要加1，因为快递默认运费占了第一个下标
            foreach ($special as $key=>$value) {
                if (empty($areas[$key])) continue;
                $areas[$key] = explode('|||',$areas[$key]);
                $trans_list[$key+1]['city'] = $areas[$key][0];
                $trans_list[$key+1]['area_name'] = $areas[$key][1];
                $trans_list[$key+1]['price_type'] = $price_type;
                $trans_list[$key+1]['express_start'] = $value['start'];
                $trans_list[$key+1]['express_postage'] = $value['postage'];
                $trans_list[$key+1]['express_plus'] = $value['plus'];
                $trans_list[$key+1]['express_postageplus'] = $value['postageplus'];
                $trans_list[$key+1]['transport_id'] = $transport_id;
                //计算省份ID
                $province = array();
                $tmp = explode(',',$areas[$key][0]);
                if (!empty($tmp) && is_array($tmp)){
                    foreach ($tmp as $k=>$v) {
                        if (!in_array($v,$province) && in_array($v,$proIds)){
                            $province[] = $v;
                        }
                    }
                }
                if (count($province)>0){
                    $trans_list[$key+1]['province'] = implode(',',$province);
                }else{
                    $trans_list[$key+1]['province'] = '';
                }
            }
        }
        
       return $trans_list; 
    }
    
    
    
}